<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="800px"
    append-to-body
  >
    <div class="form-box">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="运营商名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入运营商名称" />
        </el-form-item>
        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请填写备注"
              :rows="4"
              style="width:600px"
            />
          </el-form-item>
        </el-row>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import gameListApi from "@/api/gameConfig/gameList";
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      title: "新增运营商",
      isEdit: false,

      // 表单参数
      form: {
        name: '',
        remark: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入运营商名称", trigger: "change" }
        ]
      }
    };
  },
  created() {
  },
  methods: {
    // 打开弹窗
    open(flag, data) {
      if (flag == "edit") {
        this.title = "编辑运营商";
        this.isEdit = true;
        this.handleUpdate(JSON.parse(JSON.stringify(data)));
      } else {
        this.isEdit = false;
      }
      this.dialogVisible = true;
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    /** 查询详情进行回显 */
    handleUpdate(row) {
      this.form = row
    },
    submitForm(formName) {
      this.$refs["" + formName].validate(valid => {
        if (valid) {
          if (this.isEdit) {
            // 编辑
            gameListApi.editGameYunyingshang(this.form).then(res => {
              this.$message.success(res.msg);
              this.reset()
              this.dialogVisible = false;
              this.$parent.getList();
            });

          } else {
            // 新增
            //console.log(this.form);
            gameListApi.addGameYunyingshang(this.form).then(res => {
              this.$message.success(res.msg);
              this.reset()
              this.dialogVisible = false;
              this.$parent.getList();
            });
          }
        } else {

          this.$alert("请填写完整！");
        }
      });
    },
    close() {
      this.reset()
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}
.form-box {
  padding-top: 20px;
}

</style>
